﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Data.SqlClient;
namespace Guest_room
{
    public partial class Default : Form
    {
        public Default()
        {
            InitializeComponent();
        }
        SqlDataAdapter daProduct;
        DataSet ds = new DataSet();//适配器对象
        Font f1 = new Font("UTF-8", 10);
        public static string pro_id;
        void init()
        {
            DB.GetCn();
            string str = "select 客户编号,客户姓名,客房编号,入住日期,退房日期,预计入住时长,实际入住时长 from 客户住宿信息";
            daProduct = new SqlDataAdapter(str, DB.cn);
            daProduct.Fill(ds, "pro_info");
            dataGridView1.Font = f1;
            DB.cn.Close();
        }
        void showAll()
        {
            DataView dvProduct = new DataView(ds.Tables["pro_info"]);
            dataGridView1.DataSource = dvProduct;
            //字体居中
            dataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //列名居中
            dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //列宽自适应列名
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            CHpwd cHpwd = new CHpwd();
            cHpwd.Show();
        }

        private void Default_Load(object sender, EventArgs e)
        {
            label2.Text = Login.username ?? Home.username;
            init();
            dvgProduct();//加载下拉编号
            showAll();//显示全部
            //dgvHead();//重绘表头
            showXz();//显示选择列
        }
        void dgvHead()
        {
            this.dataGridView1.Columns[0].HeaderText = "客户编号";
            this.dataGridView1.Columns[1].HeaderText = "客户姓名";
            this.dataGridView1.Columns[2].HeaderText = "客房编号";
            this.dataGridView1.Columns[3].HeaderText = "入住日期";
            this.dataGridView1.Columns[4].HeaderText = "入住经办员编号";
            this.dataGridView1.Columns[5].HeaderText = "退房日期";
            this.dataGridView1.Columns[6].HeaderText = "退房经办员编号";
            this.dataGridView1.Columns[7].HeaderText = "入住时长";
            this.dataGridView1.Columns[8].HeaderText = "房间金额";
        }
        void showXz()//显示选择列
        {
            DataGridViewCheckBoxColumn acCode = new DataGridViewCheckBoxColumn();
            acCode.Name = "acCode";
            acCode.HeaderText = "选择";

            //显示在第一列
            dataGridView1.Columns.Insert(0, acCode);
            //宽度为40
            dataGridView1.Columns[0].Width = 50;
        }
        void dvgProduct()//加载客户编号
        {
            DataView dvProfuct = new DataView(ds.Tables["pro_info"]);
            comboBox1.DisplayMember = "客户编号";
            comboBox1.ValueMember = "客户编号";
            comboBox1.DataSource = dvProfuct;//绑定数据源
        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)//按编号查询
        {
            DataView dvProId = new DataView(ds.Tables["pro_info"]);//创建数据视图
            dvProId.RowFilter = "客户编号='" + comboBox1.SelectedValue.ToString() + "'";//过滤数据
            dataGridView1.DataSource = dvProId;//绑定数据源
        }

        private void button3_Click(object sender, EventArgs e)
        {
            showAll();
        }

        private void button2_Click(object sender, EventArgs e)//按姓名查询
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请输入关键字");
            }
            else
            {
                DataView dvName = new DataView(ds.Tables["pro_info"]);  //创建数据视图
                dvName.RowFilter = "客户姓名 like'%" + textBox1.Text + "%'";    //过滤数据
                if (dvName.Count < 1)
                {
                    MessageBox.Show("没有找到与之匹配的结果！");
                }
                else
                {
                    dataGridView1.DataSource = dvName;
                }
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)   //判断是否有数据
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)  //遍历所有行
                {
                    DataGridViewCheckBoxCell ck = dataGridView1.Rows[i].Cells["acCode"] as DataGridViewCheckBoxCell;    //获取当前行的选择框
                    if (i != e.RowIndex)    //判断是否为当前行
                    {
                        ck.Value = false;
                    }
                    else    //当前行
                    {
                        ck.Value = true;
                    }
                }
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            bool anyRowChecked = false; // 是否有任何一行被勾选
            if (dataGridView1.Rows.Count > 0)
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)  //遍历所有行
                {
                    string _selectValue = dataGridView1.Rows[i].Cells["acCode"].EditedFormattedValue.ToString();    // 获取选择框的值

                    if (_selectValue == "True") // 如果被勾选
                    {
                        anyRowChecked = true;   // 有行被勾选
                        pro_id = dataGridView1.Rows[i].Cells["客户姓名"].Value.ToString();  // 获取当前行的产品编号
                        break; // 一旦发现有勾选，则退出循环
                    }
                }
            }
            else
            {
                MessageBox.Show("没有数据");
                return;
            }

            if (!anyRowChecked || pro_id == null || pro_id == "")
            {
                MessageBox.Show("请选择一行数据");
                return;
            }
            CHX_Product chX_Product = new CHX_Product();
            chX_Product.ShowDialog();
        }

        private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell ck = dataGridView1.Rows[i].Cells["acCode"] as DataGridViewCheckBoxCell;
                    ck.Value = (i != e.RowIndex) ? false : true;
                }
            }
        }

    }
}
